#ifndef EVENT_LOOP_H
#define EVENT_LOOP_H

#include "Macro.h"
#include "TcpConnect.h"
#include "Acceptor.h"
#include <sys/epoll.h>
#include <vector>
#include <map>
#include <memory>
#include <functional>
#include <utility>

using std::cout;
using std::endl;
using std::function;
using std::make_pair;
using std::make_shared;
using std::map;
using std::shared_ptr;
using std::vector;

class EventLoop
{
    using TcpConnectPtr = shared_ptr<TcpConnect>;
    using Callback = function<void(TcpConnectPtr)>;

private:
    int _epfd;
    bool _isLooping;
    Acceptor &_acc;
    vector<epoll_event> _evts;
    map<int, TcpConnectPtr> _tcps;

    Callback _connectCallback;
    Callback _handleMessageCallback;
    Callback _closeCallback;

private:
    void addSocket() const;
    bool isClosed(int) const;

public:
    EventLoop(Acceptor &acc);
    void loop();
    void unloop();
    void epollWait();
    void addEventCtl(int) const;
    void addTcp(int);
    void delEventClt(int);
    void delTcp(const map<int, TcpConnectPtr>::iterator &);
    void handleMessage(int);

    void setConnectCallback(Callback &&cb);
    void setHandleMessage(Callback &&cb);
    void setCloseCallback(Callback &&cb);
};

#endif